
def add_text_to_video(video_path: str, output_path: str, texts: List[Dict[str, Any]]) -> bool:
    try:
        # 检查视频文件是否存在
        if not os.path.exists(video_path):
            print(f"视频文件不存在: {video_path}")
            return False
            
        # 加载视频
        video = VideoFileClip(video_path)
        width, height = video.size
        
        # 创建文本剪辑
        text_clips = []
        
        # 处理每个文字
        for text_config in texts:
            text = text_config.get('text', '')
            position = text_config.get('position', ('center', 'center'))
            fontsize = text_config.get('fontsize', 50)
            color = text_config.get('color', 'white')
            duration = text_config.get('duration', video.duration)
            start_time = text_config.get('start_time', 0)
            effect = text_config.get('effect', None)
            
            # 创建文本剪辑（直接使用TextClip而不是通过图像）
            try:
                # 支持emoji和特殊字符
                text_clip = TextClip(text=text, font_size=fontsize, color=color, font="Arial Unicode MS")
                
                # 设置字体为黄色粗体
                if color == 'yellow_bold':
                    text_clip = text_clip.set_color((255, 215, 0))  # 黄色
                    text_clip = text_clip.set_stroke(color='black', stroke_width=2)  # 粗体效果
                
                # 调整文本框高度，确保所有字符都可见
                text_clip = text_clip.set_position(position).set_duration(duration)
                
                # 如果有开始时间，则设置开始时间
                if start_time > 0:
                    if hasattr(text_clip, 'with_start'):
                        # MoviePy 2.x
                        text_clip = text_clip.with_start(start_time)
                    else:
                        # MoviePy 1.x
                        text_clip = text_clip.set_start(start_time)
                
                # 添加动画效果
                if effect == 'fadein' and fadein is not None:
                    text_clip = fadein(text_clip, 0.5)
                elif effect == 'fadeout' and fadeout is not None:
                    text_clip = fadeout(text_clip, 0.5)
                elif effect == 'fadeinout' and fadein is not None and fadeout is not None:
                    text_clip = fadeout(fadein(text_clip, 0.5), 0.5)
                
                text_clips.append(text_clip)
            except Exception as e:
                print(f"创建文本剪辑时出错: {e}")
                return False
        
        # 合成最终视频
        final_clips = [video] + text_clips
        final_video = CompositeVideoClip(final_clips)
        
        # 确保输出目录存在
        output_dir = os.path.dirname(output_path)
        if output_dir:
            os.makedirs(output_dir, exist_ok=True)
        
        # 输出视频
        print(f"正在生成视频: {output_path}")
        final_video.write_videofile(output_path, codec='libx264', audio_codec='aac', threads=8)
        
        return True
    except Exception as e:
        print(f"在视频上添加文字时出错: {e}")
        return False
    finally:
        # 关闭视频剪辑以释放资源
        if video:
            video.close()
        if final_video:
            final_video.close()
        for clip in text_clips:
            if clip:
                clip.close()
